package com.wanlian.dao.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.WordBookDao;
import com.wanlian.entity.WordBook;
import com.wanlian.model.ResultModel;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class WordBookDaoImpl extends GenericDaoImpl<WordBook> implements WordBookDao {

    public WordBookDaoImpl() {
        super(WordBook.class);
    }


    @Override
    public WordBook queryWordBookByNameId(String wbname, String wbval2) {
        String hql = String.format(" from %s where wbname=? and wbval2=? and  isenable=1  ", entityClassName());
        WordBook wordBook = this.queryForTopObject(hql, new Object[]{wbname, wbval2});
        return wordBook;
    }

    @Override
    public WordBook queryWordBookByVal2(String wbval2) {
        String hql = String.format(" from %s where wbval2=?  ", entityClassName());
        WordBook wordBook = this.queryForTopObject(hql, new Object[]{wbval2});
        return wordBook;
    }

    @Override
    public List<WordBook> queryWordBooksByVal2(String wbval2) {
        String hql = String.format(" from %s where wbval2=?  ", entityClassName());
        List<WordBook> wordBooks = this.queryForList(hql, new Object[]{wbval2});
        return wordBooks;
    }

    @Override
    public ResultModel queryWordBookList(PageCondition pageCondition) {
        ResultModel resultModel = new ResultModel();
        resultModel.setBresult(true);
        StringBuffer sqlWhere = new StringBuffer(" where 1 = 1");
        ArrayList<Object> params = new ArrayList<Object>();

        if (pageCondition.isEmpty("wbname")) {
            String wbname = (String) pageCondition.getByKey("wbname");
            sqlWhere.append(" and wbname like ?");
            params.add("%" + wbname + "%");
        }

        if (pageCondition.isEmpty("wbval2")) {
            String wbval2 = (String) pageCondition.getByKey("wbval2");
            sqlWhere.append(" and wbval2 like ?");
            params.add("%" + wbval2 + "%");
        }

        if (pageCondition.isEmpty("wbtype")) {
            String wbtype = (String) pageCondition.getByKey("wbtype");
            sqlWhere.append(" and wbtype like ?");
            params.add("%" + wbtype + "%");
        }

        if (pageCondition.isEmpty("wbtype")) {
            String wbtype = (String) pageCondition.getByKey("wbtype");
            sqlWhere.append(" and wbtype = ?");
            params.add(wbtype);
        }

        if (pageCondition.isEmpty("tpname")) {
            String tpname = (String) pageCondition.getByKey("tpname");
            sqlWhere.append(" and tpname = ?");
            params.add(tpname);
        }

        if (pageCondition.isEmpty("isEnable")) {
            Boolean isEnable = (Boolean) pageCondition.getByKey("isEnable");
            sqlWhere.append(" and isenable = ?");
            params.add(isEnable);
        }

        if (pageCondition.isEmpty("userId")) {
            int userId = Integer.parseInt((String) pageCondition.getByKey("userId"));
            sqlWhere.append(" and userId = ?");
            params.add(userId);
        }

        List<WordBook> lists = new ArrayList<WordBook>();
        String hql = String.format(" from %s  ", entityClassName(), sqlWhere.toString());
        lists = this.queryForListByPage(hql + sqlWhere, params.toArray(), pageCondition.getPage());
        resultModel.setObject(lists);
        String countHql = String.format("select count(*) from  %s " + sqlWhere, entityClassName(), params.toString());
        Long count = this.queryCount(countHql, params.toArray());
        resultModel.setCount(count);
        return resultModel;
    }

    //该方法用于判断修改后的值是否与原字典值重复
    @Override
    public WordBook queryWordBookFlag(WordBook wordBook) {
        String wbname = wordBook.getWbname();
        String wbval2 = wordBook.getWbval2();
        int id = wordBook.getId();
        String hql = String.format(" from %s where wbname=? and wbval2=? and id!=? and isenable=true  ", entityClassName());
        WordBook dbwordBook = this.queryForTopObject(hql, new Object[]{wbname, wbval2, id});
        return dbwordBook;
    }

    @Override
    public List<WordBook> queryChannel() {
        String hql = String.format(" from %s where  isenable=? and (wbval2='online_channel' or wbval2='offline_channel')  ", entityClassName());
        List<WordBook> wordBooks = this.queryForList(hql, new Object[]{true});
        return wordBooks;
    }

    @Override
    public List<String> queryWbval1sByVal2(String wbval2) {
        List<WordBook> wordBooks = this.queryWordBooksByVal2(wbval2);
        List<String> wordBookStrings = new ArrayList<>();
        for (WordBook item : wordBooks) {
            wordBookStrings.add(item.getWbval1());
        }
        return wordBookStrings;
    }
}
